<?php
namespace Storecenter\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        $username = session('username');
        $store = M('Store');
        $storeData = $store->field(array('id', 'shop_name', 'shop_notice', 'discount', 'shop_phone', 'price_tosend'))->where(array('username' => $username))->find();
        //var_dump($storeData);
        $order = M('Order');
        $dish = M('Dish');
        $address = M('Address');
        //接单↓
        $orderAccept = $order->field(array('id','setorder_time', 'dish_id_string', 'address_id', 'total_price', 'remark'))->where(array('store_id' => $storeData['id'], 'state' => '11'))->order('setorder_time')->select();
        //var_dump($orderAccept);
        foreach ($orderAccept as $key => $value) {
            $dishString = $value['dish_id_string'];
            $dishList = explode('-', $dishString);
            $addressInfo = $address->field(array('address', 'phone', 'name'))->where(array('id' => $value['address_id']))->find();
            $orderAccept[$key]['address']=$addressInfo['address'];
            $orderAccept[$key]['phone']=$addressInfo['phone'];
            $orderAccept[$key]['name']=$addressInfo['name'];
            foreach ($dishList as $k => $v) {
                $dishId_Num = explode(':', $v);
                $dishName = $dish->field('dish_name')->where(array('id' => $dishId_Num[0]))->find();
                $dishList[$k] = $dishName['dish_name'] . ' x' . $dishId_Num[1] . '份';
            }
            $orderAccept[$key]['dishList'] = $dishList;
        }
        /*print_r($orderAccept);*/
        //退单↓
        $orderRefuse = $order->field(array('id','setorder_time', 'dish_id_string', 'address_id', 'total_price', 'remark'))->where(array('store_id' => $storeData['id'], 'state' => '1110'))->order('setorder_time')->select();
        foreach ($orderRefuse as $key => $value) {
            $dishString = $value['dish_id_string'];
            $dishList = explode('-', $dishString);
            $addressInfo = $address->field(array('address', 'phone', 'name'))->where(array('id' => $value['address_id']))->find();
            $orderRefuse[$key]['address']=$addressInfo['address'];
            $orderRefuse[$key]['phone']=$addressInfo['phone'];
            $orderRefuse[$key]['name']=$addressInfo['name'];
            foreach ($dishList as $k => $v) {
                $dishId_Num = explode(':', $v);
                $dishName = $dish->field('dish_name')->where(array('id' => $dishId_Num[0]))->find();
                $dishList[$k] = $dishName['dish_name'] . ' x' . $dishId_Num[1] . '份';
            }
            $orderRefuse[$key]['dishList'] = $dishList;
        }
        $this->assign('username', $username);
        $this->assign('orderAccept', $orderAccept);
        $this->assign('orderRefuse', $orderRefuse);
        $this->assign('storeData', $storeData);
        $this->display();
    }

    public function signin()
    {
        $this->display();
    }
}